package com.powernode.lambda.lambda;

import com.powernode.lambda.Student;

import java.util.ArrayList;

public class Lambda01 {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("乔峰",40,"男"));
        students.add(new Student("阿紫",20,"女"));
        students.add(new Student("阿朱",23,"女"));
        students.add(new Student("段誉",25,"男"));

        //一般做法
        System.out.println(filter2(students));
        System.out.println(filter3(students));
        System.out.println("================");
        //运用函数式编程
        System.out.println(filter0(students,student -> student.getAge()>=23));

    }

    public static ArrayList<Student> filter2(ArrayList<Student> students){
        ArrayList<Student> rt = new ArrayList<>();
        for (Student student : students) {
            if (student.getSex().equals("女")){
                rt.add(student);
            }
        }
        return rt;
    }

    public static ArrayList<Student> filter3(ArrayList<Student> students){
        ArrayList<Student> rt = new ArrayList<>();
        for (Student student : students) {
            if (student.getAge()>=25){
                rt.add(student);
            }
        }
        return rt;
    }


    interface filter{
        boolean rule(Student student);
    }

    /**
     * 把接口当做参数传进去，即把过滤规则当做参数传进去
     * @param students
     * @param filter
     * @return
     */
    public static ArrayList<Student> filter0(ArrayList<Student> students,filter filter){
        ArrayList<Student> rt = new ArrayList<>();
        for (Student student : students) {
            if (filter.rule(student)){
                rt.add(student);
            }
        }
        return rt;
    }


}
